<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="ct-repository-commit-list.html">

<script>
function CTCommitList(repositories, revisions) {
  this._initRevisions(revisions);
  this.update(repositories);
}

CTCommitList.prototype.update = function(repos) {
  this.repositories = [];
  if (!repos)
    return;

  Object.keys(repos.repositories, function(repoName, repository) {
    if (!this.revisions[repoName])
      return;
    var commits = repository.range(this.revisions[repoName]);
    if (!commits.length)
      return;

    this.repositories.push(new CTRepositoryCommitList(repoName, commits));
  }.bind(this));
};

CTCommitList.prototype._initRevisions = function(repoRevisions) {
  this.revisions = {};
  repoRevisions.forEach(function(repoRevision) {
    var split = repoRevision.split(':');
    var repo = split[0];
    var revision = parseInt(split[1], 10);
    if (revision && repo) {
      if (!this.revisions[repo]) {
        this.revisions[repo] = [revision];
      } else {
        this.revisions[repo].push(revision);
      }
    }
  }.bind(this));
};

</script>
